<?php
require_once 'Zend/Search/Lucene.php';
class SearchController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {

    }

    public function indexerAction() {
        $productObj = new Application_Model_Product();
        $products = $productObj->GetAllProducts();
        $index = Zend_Search_Lucene::create(APPLICATION_PATH.'/../product-index');
        foreach ($products as $product) {
            $doc = new Zend_Search_Lucene_Document();
            $doc->addField(Zend_Search_Lucene_Field::unIndexed('url',
                            $product["id"]));
            $doc->addField(Zend_Search_Lucene_Field::Text('name',
                            $product["name"]));
            $doc->addField(Zend_Search_Lucene_Field::Text('contents',
                            $product["discripion"]));
            $doc->addField(Zend_Search_Lucene_Field::Text('category',
                            $product["category_id"]));
            $doc->addField(Zend_Search_Lucene_Field::Text('title',
                            $product["title"]));
            $doc->addField(Zend_Search_Lucene_Field::unIndexed('image',
                            $product["image"]));
            $index->addDocument($doc);
        }
        $index->commit();
        $index->optimize();
    }

    public function searchAction() {
        $query = $this->_getParam('q');
        $index = Zend_Search_Lucene::open(APPLICATION_PATH.'/../product-index');
        $hits = $index->find($query);
        $adapter = new Zend_Paginator_Adapter_Array($hits);
        $paging = new Zend_Paginator($adapter);
        $paging->setItemCountPerPage(6);
        $page = $this->_request->getParam('page', 1);
        $paging->setCurrentPageNumber($page);
        $this->view->hits = $paging;
    } 

}

